<?php
class AccountDb
{
	protected $db; // database
	protected $wr; // Wrapper for table 'account'

	function __construct()
	{
		$this->db = Zend_Registry::get('database');
		$this->wr = new Wrapper('account', 'AId',
				'AId|User:用户名|Pass:密码|Priv|Sort:排序号|Status:身份|'.
				'Name:姓名|Icon:头像|Race:民族|Birth:生日|Sex:性别|'.
				'Eduction:学历|Title:称呼|Intro:详细介绍|'.
				'Interest:研究方向|MSN|Email|Tel:电话|Home:主页|'.
				'Blog|QQ|Job:工作|EnrollDate:入学日期',
			array('User'=>'/(\w{4,})/', 'Pass'=>'/(.{6,32})/') );
	}

	/// 取得上次错误的字段
	function Field()
	{
		return $this->wr->errField;
	}

	/// 取得上次错误信息
	function Error()
	{
		return $this->wr->error;
	}

	/// 检测字段是否符合规范，成功返回过滤后的数组，否则返回错误码，
	/// 并可以通过函数：AccountDb::Field()取得检测失败字段，或通过函
	/// 数AccountDb::Error()取得错误信息。
	function Check($data)
	{
		return $this->wr->Check($data);
	}

	/// 插入一个人信息，成功返回id，失败返回<0
	function Insert($data)
	{
		return $this->wr->Insert($this->db, $data);
	}

	/// 更新一个人信息，成功返回1，失败返回0
	function Update($aid, $data)
	{
		return $this->wr->Update($this->db, $aid, $data);
	}

	/// 删除一个人
	function Delete($aid)
	{
		return $this->wr->Delete($this->db, $aid);
	}

	/// Status
	static function Status($val)
	{
		if ($val>7 || $val<0) return '其他';
		$arr = array(0=>'毕业生', 1=>'在读硕士', 2=>'在读博士',
					 3=>'教师', 4=>'讲师', 5=>'副教授',
					 6=>'教授', 7=>'教授、博士生导师');
		return $arr[$val];
	}

	/// Sex
	static function Sex($val)
	{
		if ($val=='m') return '男';
		if ($val=='f') return '女';
		return '';
	}

	/// 格式化个人信息
	static function Format($rows)
	{
		$result = array();
		foreach ($rows as $row) {

			foreach ($row as $k => $v) {
				switch ($k) {
				case 'Sex':
					$d['Sex'] = self::Sex($v);
					break;
				case 'Status':
					$d['Status'] = self::Status($v);
					break;
				case 'Birth':
					if (empty($v)) $d[$k] = '';
					else $d[$k] = strftime('%Y年生', $v);
					break;
				case 'EnrollDate':
					if (empty($v)) $d[$k] = '';
					else $d[$k] = strftime('%Y年9月', $v);
					break;
				case 'Home':
				case 'Blog':
					$d[$k] = Wrapper::Link(Wrapper::Escape($v));
					break;
				default:
					$d[$k] = Wrapper::Escape($v);
				}
			}
			$result[] = $d;

		} // end of $rows
		return $result;
	}

	/// 查询个人的信息，$aid==0则查出所有人。
	function Fetch($aid)
	{
		$aid = intval($aid);
		$where = $aid?" where AId=$aid ":' ';
		$result = $this->db->query("select * from account $where");
		return $result->fetchAll(Zend_Db::FETCH_ASSOC);
	}

	/// fetch all teachers
	function Teacher()
	{
		$result = $this->db->query('select * from account where `Status`>=3 '.
								   'order by Sort desc, AId asc');
		return self::Format($result->fetchAll());
	}

	/// fetch all phd
	function Phd()
	{
		$result = $this->db->query('select * from account where `Status`=2');
		return $result->fetchAll();
	}

	/// fetch all master student
	function Master()
	{
		$result = $this->db->query('select * from account where `Status`=1');
		return $result->fetchAll();
	}

	/// fetch all graduate
	function Graduate()
	{
		$result = $this->db->query('select * from account where `Status`=0');
		return $result->fetchAll();
	}


};
?>
